﻿using System.Collections.ObjectModel;
using System.Linq;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// The filtered tournaments that should be shown
    /// </summary>
    public class TournamentsViewModel : ViewModelBase
    {
        /// <summary>
        /// The filtered tournaments that should be shown
        /// </summary>
        public ObservableCollection<TournamentViewModel> Tournaments { get; private set; }
        
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="tournamentsModel">The source unfiltered data model</param>
        public TournamentsViewModel(TournamentsModel tournamentsModel)
        {
            Tournaments = new ObservableCollection<TournamentViewModel>();
            _tournamentsModel = tournamentsModel;
        }

        /// <summary>
        /// Populate self with all tournaments for the selected group and happened in the selected season
        /// </summary>
        /// <param name="selectedGroup">e.g. "Men 14-16"</param>
        /// <param name="selectedSeason">e.g. "2010"</param>
        /// <param name="notifySelection">Methods to call if the user selects something</param>
        public void Query(string selectedGroup, string selectedSeason, INotifySelection notifySelection)
        {
            Tournaments.Clear();

            foreach (var tournamentModel in
                from tournamentModel in _tournamentsModel
                where tournamentModel.Contains(selectedGroup, selectedSeason)
                select tournamentModel)
            {
                var tournamentViewModel = new TournamentViewModel(tournamentModel, notifySelection);
                Tournaments.Add(tournamentViewModel);
            }
        }

        readonly TournamentsModel _tournamentsModel = new TournamentsModel();
    }
}
